<template>
  <div class="add-form">
    <el-dialog :title="titleInfo" :visible.sync="dialogFormVisible" class='small-add-dialog'>
      <el-form
        :rules="ruleInline"
        ref="dataForm"
        :model="formBase"
        label-position="left"
        label-width="80px"
        style="width: 80%; margin-left:10px;"
      >
        <el-form-item label="所属学科" prop="subjectID" v-if="!isSingle">
          <el-select v-model="formBase.subjectID" placeholder="请选择">
            <el-option
              v-for="item in subjectOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="目录名称" prop="directoryName">
          <el-input v-model="formBase.directoryName">
          </el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormH">{{$t('table.cancel')}}</el-button>
        <el-button type="primary" @click="createData">{{$t('table.confirm')}}</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { update, add } from '@/api/hmmm/directorys.js'/* 修改、添加 */
import { simple } from '@/api/hmmm/subjects.js'/* 学科简单列表 */
export default {
  name: 'CompanysAdd',
  props: {
    titleInfo: {
      required: true
    },
    formBase: {
      type: Object,
      required: true
    },
    isSingle: {
      type: Boolean
    }
  },
  data () {
    return {
      dialogFormVisible: false,
      // 简单学科数据
      subjectOptions: [],
      // 表单验证
      ruleInline: {
        subjectID: [
          { required: true, message: '学科名称不能为空', trigger: 'blur' }
        ],
        directoryName: [
          { required: true, message: '目录名称不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  async created () {
    const { data } = await simple()
    this.subjectOptions = data
  },
  methods: {
    // 弹层显示
    dialogFormV () {
      this.dialogFormVisible = true
    },
    // 弹层隐藏
    dialogFormH () {
      this.dialogFormVisible = false
    },

    // 表单提交
    createData () {
      this.$refs.dataForm.validate(valid => {
        if (valid) {
          this.dialogFormH()/* 隐藏 */
          const data = {
            ...this.formBase
          }
          if (this.formBase.id) {
            // 更新
            update(data).then(() => {
              this.$emit('newDataes', this.formBase)
            })
            // 提示
            this.$message({
              message: '修改成功',
              type: 'success'
            })
          } else {
            // 添加
            add(data).then(() => {
              this.$emit('newDataes', this.formBase)
            })
            // 提示
            this.$message({
              message: '添加成功',
              type: 'success'
            })
          }
        } else {
          this.$message.error('请输入要添加的学科!')
        }
      })
      this.$refs.dataForm.resetFields()/* 重置 */
    }
  }
}
</script>
<style>
.el-form--label-left .el-form-item__label {
  text-align: right;
}
.el-form-item--medium {
  margin-bottom: 22px;
}
.el-dialog__footer {
  text-align: center;
}
</style>
